package com.dianrui.agent.activity;

import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.JsonUtils;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class DeviceInfoActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.device_code)
    TextView deviceCode;
    @BindView(R.id.brand_type)
    TextView brandType;
    @BindView(R.id.remark)
    TextView remark;
    @BindView(R.id.create_time)
    TextView createTime;
    @BindView(R.id.number)
    TextView number;
    @BindView(R.id.mileage)
    TextView mileage;
    @BindView(R.id.order_number)
    TextView orderNumber;
    private String vehicleId;



    @Override
    public int getContentViewId() {
        return R.layout.activity_device_info;
    }

    @Override
    public void initView() {
        title.setText("设备详情");
        if (getIntent() != null && getIntent().getExtras()!=null) {
            vehicleId=getIntent().getStringExtra("vehicleId");

        }
        getVehicle();

    }

    private void getVehicle() {
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("vehicle_id",vehicleId);
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl()+Url.VECHINEDETAILS, jsonObject.toString(), new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject=new JSONObject(result);
                        if (jsonObject != null) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                String data= JsonUtils.getJsonStr(result,"data");

                                StringBuilder deviceCodeString = new StringBuilder();
                                deviceCodeString.append("设备编号:").append(JsonUtils.getJsonStr(data,"number"));
                                deviceCode.setText(deviceCodeString.toString());

                                StringBuilder createtimeString = new StringBuilder();
                                createtimeString.append("投入时间: ").append(JsonUtils.getJsonStr(data,"create_time"));
                                createTime.setText(createtimeString.toString());

                                StringBuilder mileageString = new StringBuilder();
                                mileageString.append("总里程数: ").append(JsonUtils.getJsonStr(data,"mileage")).append("公里");
                                mileage.setText(mileageString.toString());


                                StringBuilder ordernumberString = new StringBuilder();
                                ordernumberString.append("总订单数: ").append(JsonUtils.getJsonStr(data,"order_number"));
                                orderNumber.setText(ordernumberString.toString());


                                StringBuilder brandtypeString = new StringBuilder();
                                brandtypeString.append("车辆类型: ").append(JsonUtils.getJsonStr(data,"type_name"));
                                brandType.setText(brandtypeString.toString());

                                if (!StringUtils.isEmpty(jsonObject.optJSONObject("data").optString("remark"))) {
                                    StringBuilder remarkString = new StringBuilder();
                                    remarkString.append("车辆牌号: ").append(JsonUtils.getJsonStr(data,"remark"));
                                    remark.setText(remarkString.toString());
                                }else{
                                    remark.setText("车辆牌号: 未上牌");
                                }

                            }else if ("1001".equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }else if ("1002".equals(jsonObject.optString("status"))) {
                                spUtils.clear();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                JumpActivitys(LoginActivity.class);
                                finish();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }



}
